<?php
uses('controller');

class forumController extends Controller {

    function __construct() {
        parent::__construct();
    }

    /*
    4.没有找到相应的数据
    8.数据不完整
    */
    function forum() {
        $data['id']     = intval($this->request['id']);
        $data['tid']    = $this->request['tid'];

        if ($data['tid'] == 'new'){
            $result = $this->newPost($data); //新发表
        } elseif (intval($data['tid'])) {
            $result = $this->topicAll($data); //话题展开
        } elseif ($data['id']) {
            $result = $this->topicList($data); //话题列表
        } else {
            $result['code']['forum'] = 4;
        }

        return $result;
    }

    /*
    2.更新错误
    4.标题为空
    8.内容为空
    16.姓名为空
    32.邮件为空
    64.非法请求
    128.验证码不对
    */
    function newPost($data=null) {
        $data['id']         = intval($this->request['id']);
        $data['tid']        = $this->request['tid'];

        //$data['subject']    = $this->cn_substr($this->_preFormat($this->request['subject']), 20); 改到输出的时候限制

        $data['subject']    = $this->_preFormat($this->request['subject']);
        $data['body']       = $this->_preFormat($this->request['body'], true);
        $data['uname']      = $this->session['uname'] ? $this->session['uname'] : $this->_preFormat($this->request['uname']);
        $data['umail']      = $this->session['umail'] ? $this->session['umail'] : $this->_preFormat($this->request['umail']);

        //验证码
        if (!$this->session['login'] && (!$this->session['rand_code'] || !$this->request['rand_code'] || ($this->request['rand_code'] != $this->session['rand_code'])) ) $result['code']['newPost'] = 128;

        if (!$data['body'])     $result['code']['newPost'] = 8;
        if (!$data['subject'])  $result['code']['newPost'] = 4;
        if (!$this->session['login'] && !preg_match($this->regexp['email'], $data['umail']))  $result['code']['newPost'] = 32;
        if (!$this->session['login'] && !$data['uname'])  $result['code']['newPost'] = 16;

        if (!$result['code']) {
            $this->session['uname'] = $data['uname'];
            $this->session['umail'] = $data['umail'];

            if ($this->session['login']) {
                $data['uid']    = $this->session['id'];
                $data['uname']  = $this->session['username'];
                $data['umail']  = $this->session['email'];
            }

            $result = $this->model->newPost($data);
            $result['code']['forum'] = $result['code']['newPost'];
        }

        if ($this->session['uname']) $data['uname'] = $this->session['uname'];
        if ($this->session['umail']) $data['umail'] = $this->session['umail'];

        $result['data'] = $data;

        return $result;
    }

    function ajaxNewPost($var) {
        parse_str($var, $this->post);
        $this->post = array_stripslashes($this->post);
        $this->request = array_merge($this->request, $this->post);
        $result = $this->newPost();

        $this->ajax->quickresult($result['code']['newPost']);
    }

    /*
    2.更新错误
    4.内容重复
    8.内容为空
    16.姓名为空
    32.邮件为空
    64.非法请求
    */
    function newReply($data=null) {
        $data['id']     = intval($this->request['id']);
        $data['tid']    = $this->request['tid'];

        $data['body']   = $this->_preFormat($this->request['body'], true);
        $data['uname']      = $this->session['uname'] ? $this->session['uname'] : $this->_preFormat($this->request['uname']);
        $data['umail']      = $this->session['umail'] ? $this->session['umail'] : $this->_preFormat($this->request['umail']);

        if (!$data['body']) $result['code']['newReply'] = 8;
        if (!$this->session['login'] && !preg_match($this->regexp['email'], $data['umail']))  $result['code']['newReply'] = 32;
        if (!$this->session['login'] && !$data['uname'])  $result['code']['newReply'] = 16;

        if (!$result['code']) {
            $this->session['uname'] = $data['uname'];
            $this->session['umail'] = $data['umail'];

            if ($this->session['login']) {
                $data['uid']    = $this->session['id'];
                $data['uname']  = $this->session['username'];
                $data['umail']  = $this->session['email'];
            }

            //管理员名称
            $data['isadmin'] = $this->session['admin'];

            $result = $this->model->newReply($data);
            $result['code']['forum'] = $result['code']['newReply'];
        }

        $result['data'] = $data;

        return $result;
    }

    function ajaxNewReply($var) {
        parse_str($var, $this->post);
        $this->post = array_stripslashes($this->post);
        $this->request = array_merge($this->request, $this->post);
        $result = $this->newReply();

        $rr =& $this->ajax->add_node('result');
        $rr->set_data($result['code']['newReply']);

        $node =& $this->ajax->add_node('fdata');

        $uname =& $node->add_node('uname');
        $uname->set_data($result['data']['uname']);

        $body =& $node->add_node('body');
        $body->set_data(nl2br($result['data']['body']));

        $date =& $node->add_node('updated');
        $date->set_data(date('Y-m-d H:i:s'));
    }

    //帖子列表
    function topicAll($data) {
        if ($this->post) {
            $rcode = $this->newReply($data);
            if ($rcode['code']['newReply'] == 1) unset($data['body']);
        }

        if ($data['remove'] = intval($this->get['remove'])) {
            $this->needAdmin();
            $this->model->delReply($data);
        }

        $result = $this->model->getTopic($data);
        $result['code']['forum']    = $result['code']['getTopic'];
        $result['code']['newReply'] = $rcode['code']['newReply'];

        if ($this->session['uname']) $data['uname'] = $this->session['uname'];
        if ($this->session['umail']) $data['umail'] = $this->session['umail'];

        $result['data'] = $data;
        return $result;
    }

    //话题列表
    function topicList($data) {
        $data['sort']       = 'updated';
        $data['sortorder']  = 'DESC';
        $data['pagesize']   = 30;

        if ($data['remove'] = intval($this->get['remove'])) {
            $this->needAdmin();
            $this->model->delPost($data);
        }

        $result = $this->model->topicList($data);
        $result['code']['forum'] = $result['code']['topicList'];

        if ($this->session['uname']) $data['uname'] = $this->session['uname'];
        if ($this->session['umail']) $data['umail'] = $this->session['umail'];

        $result['data'] = $data;

        return $result;
    }

    //内容过滤
    function _preFormat($content, $allowhtml=false) {
        return $allowhtml ? trim(strip_tags($content, '<a><b><i><u>')) : trim(strip_tags($content));
    }

    //%3 for utf8
    function cn_substr($str,$strlen=10,$other=false) {
        $strlen *= 3;
        for($i=0;$i<$strlen;$i++)
          if(ord(substr($str,$i,1))>0xa0) $j++;
        if($j%3!=0) $strlen++;
        $rstr=substr($str,0,$strlen);
        if ((strlen($str)>$strlen) && $other) {$rstr.='……';}
        return $rstr;
    }
}
?>
